package org.dromara.mall.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.domain.MallGoodsCategory;
import org.dromara.mall.domain.vo.CategoryVo;
import org.dromara.mall.service.IMallGoodsCategoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类
 *
 * @author MrLei
 * @date 2024-05-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/goodsCategory")
@Slf4j
public class MallGoodsCategoryController extends BaseController {

    private final IMallGoodsCategoryService mallGoodsCategoryService;

    /**
     * 查询商品分类列表
     */
    @SaCheckPermission("mall:goodsCategory:list")
    @GetMapping("/list")
    public R<List<MallGoodsCategory>> list(MallGoodsCategory bo) {
        List<MallGoodsCategory> list = mallGoodsCategoryService.queryList(bo);
        return R.ok(list);
    }


    /**
     * 导出商品分类列表
     */
    @SaCheckPermission("mall:goodsCategory:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallGoodsCategory bo, HttpServletResponse response) {
        List<MallGoodsCategory> list = mallGoodsCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品分类", MallGoodsCategory.class, response);
    }

    /**
     * 获取商品分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:goodsCategory:query")
    @GetMapping("/{id}")
    public R<MallGoodsCategory> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(mallGoodsCategoryService.queryById(id));
    }

    /**
     * 新增商品分类
     */
    @SaCheckPermission("mall:goodsCategory:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MallGoodsCategory bo) {
        return toAjax(mallGoodsCategoryService.insertByBo(bo));
    }

    /**
     * 修改商品分类
     */
    @SaCheckPermission("mall:goodsCategory:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallGoodsCategory bo) {
        return toAjax(mallGoodsCategoryService.updateByBo(bo));
    }

    /**
     * 删除商品分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:goodsCategory:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallGoodsCategoryService.deleteWithValidByIds(List.of(ids), true));
    }
    /*获取父级分类ID*/
    @SaCheckPermission("mall:goodsCategory:parent")
    @GetMapping("/parent/{id}")
    public R<CategoryVo> getParent(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(mallGoodsCategoryService.selectParentId(id));
    }
}
